﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using JESAI.Platform.WebApi.Modules;
using JESAI.Schedule.HangFire;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using JESAI.ProcessMetrics.AseNetCore;
using JESAI.ProcessMetrics;
using Microsoft.AspNetCore;
using JESAI.Core.Aspects;
using Sys.Workflow.Services.Rest;
using Microsoft.OpenApi.Models;

namespace JESAI.Platform.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {

            //添加对象序列化程序为Newtonsoft.Json
            services.AddControllers().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });

            services.AddControllers();
            // 添加跨域
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", builder => builder
                    //.AllowAnyOrigin()
                    .SetIsOriginAllowed(_ => true)
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials()
                );
            });

            services.AddJESAI()
              .AddModule<WorkFlowModule>()
              .AddModule<LogModule>()
              .AddModule<MonacoRoslynModule>()
              .AddModule<DynamicCodeRunModule>()
              .AddModule<MapperModule>()
              .AddModule<CachingModule>()
              .AddModule<MQTTModule>()
              .AddModule<AppModule>()
              .AddModule<DynamicDataSourceModule>()
              .AddModule<HangfireModule>();

            services.AddEndpointsApiExplorer();

            services.AddSwaggerGen();
            //services.AddSwaggerGen(c =>
            //{
            //    c.SwaggerDoc("v1", new OpenApiInfo { Title = "平台API", Version = "v1" });
            //});
            services.AddRouting();
        }
        public void Configure(IApplicationBuilder app)
        {

            //app.UseEndpoints(endpoints =>
            //{
            //    endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
            //});

            // 使用跨域
            app.UseCors("CorsPolicy");

            // 静态文件
            app.UseDefaultFiles();
            app.UseStaticFiles();


            app.UseSwagger();
            app.UseSwaggerUI();

            app.UseJESAIExceptionHandling();
            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();

            app.UseJESAI();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapControllers();

                //endpoints.MapMqtt("/mqtt11");
                endpoints.ProcessMetrices("/metrics", options =>
                {
                    // 监控 CLR 中的事件
                    options.ListenerNames.Add(EventNames.System_Runtime);
                    options.ListenerNames.Add(EventNames.AspNetCore_Http_Connections);

                    // options.Labels.Add("other", "自定义标识");

                    // 自定义要监控的数据源
                    options.Assemblies.Add(typeof(JESAI.ProcessMetrics.MetricsPush).Assembly);
                });
            });
            //app.UseMvc(routes =>
            //{
            //    routes.MapRoute("areaRoute", "view/{area:exists}/{controller}/{action=Index}/{id?}");
            //    routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
            //    routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" });
            //});
            Console.WriteLine("==================================================================================================================");
            using (StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Banner.txt"))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }

            Console.WriteLine("==================================================================================================================");

            var _logger = ServiceLocator.Instance.GetServices<ILogger<Startup>>().FirstOrDefault();
            _logger.LogInformation("服务器重启···································");

        }
    }
}
